package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.bean.MyFocusShopBean;
import cn.xfdzx.android.apps.shop.bean.MyFollowBean;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.MyFocusShopPresenter;
import cn.xfdzx.android.apps.shop.presenter.MyFollowPresenter;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.util.TimeUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * Creator: zf.
 * Date: 2020/7/21.
 * Description:浏览记录批发店铺.
 */
public class MyFollow_XFDAdapter extends RecyclerView.Adapter {

    private ItemClickListener mItemClickListener;
    private List<MyFollowBean.DataBean.WholesaleBean> mList;
    private Activity mContext;
    private MyFollowPresenter mPresenter;
    private final LayoutInflater mLayoutInflater;
    /**
     * 布局两种类型
     */
    public static final int TYPE_1 = 0;
    public static final int TYPE_2 = 1;
    public MyFollow_XFDAdapter(Activity context,
                               List<MyFollowBean.DataBean.WholesaleBean> list,
                               MyFollowPresenter mPresenter) {
        this.mList = list;
        this.mContext = context;
        this.mPresenter = mPresenter;
        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow, parent, false);
//        MyFollow_RetailAdapter.MyViewHolder myHolder = new MyFollow_RetailAdapter.MyViewHolder(inflate);
//
//        return myHolder;

        if (viewType == TYPE_1) {
            return new MyFollow_XFDAdapter.MyViewHolder2( mLayoutInflater.inflate(R.layout.item_followtitle, null));
        } else if (viewType == TYPE_2) {
            return new MyFollow_XFDAdapter.MyViewHolder( mLayoutInflater.inflate(R.layout.item_follow, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_2) {
            MyFollow_XFDAdapter.MyViewHolder myViewHolder = (MyFollow_XFDAdapter.MyViewHolder) holder;

            myViewHolder.tv_goods_name.setText(mList.get(position).getTitle());
            if (Double.parseDouble(mList.get(position).getDiscountprice())>0){
                myViewHolder.tv_price.setText("¥"+mList.get(position).getDiscountprice());
            }else {
                myViewHolder.tv_price.setText("¥"+mList.get(position).getMarketprice());
            }
            myViewHolder.tv_shop_month_sales.setText("已售"+mList.get(position).getSale_num()+"件");
            ImageGlideUtil.loadImage(mContext,myViewHolder.iv_home_shop_list,mList.get(position).getThumb());


            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        //mItemClickListener.onItemClick(position);
                        Intent intent = new Intent(mContext, XFDGoodsDeatailsActivity.class);
                        intent.putExtra("good_id",mList.get(position).getId()+"");
                        mContext.startActivity(intent);
                    }
                }
            });

            myViewHolder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        Map<String,Object> map = new HashMap<>();
                        map.put("goods_id",mList.get(position).getId()+"");
                        map.put("type","0");
                        mPresenter.getDeleteFollow(map);
                    }
                }
            });

            myViewHolder.tv_open.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        mItemClickListener.onItemClick(position);
                    }
                }
            });
        }else if (getItemViewType(position) == TYPE_1) {
            MyFollow_XFDAdapter.MyViewHolder2 myViewHolder = (MyFollow_XFDAdapter.MyViewHolder2) holder;
            myViewHolder.tv_time.setText(TimeUtil.millis2String(mList.get(position).getTime()*1000L,"MM月dd日")+"");
            if (position>0){
                if (TimeUtil.isSameDay(mList.get(position).getTime()*1000L,mList.get(position-1).getTime()*1000L)){
                    myViewHolder.tv_time.setVisibility(View.GONE);
                    myViewHolder.view_line.setVisibility(View.GONE);
                }else {
                    myViewHolder.tv_time.setVisibility(View.VISIBLE);
                    myViewHolder.view_line.setVisibility(View.VISIBLE);
                }
            }else {
                if (TimeUtil.isSameDay(mList.get(position).getTime()*1000L,System.currentTimeMillis())){
                    myViewHolder.tv_time.setText("今天");
                }
            }
        }


    }

    public void setDate(List<MyFollowBean.DataBean.WholesaleBean> list){
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).getType() == 0) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_home_shop_list)
        ImageView iv_home_shop_list;
        @BindView(R.id.tv_goods_name)
        TextView tv_goods_name;
        @BindView(R.id.tv_shop_level)
        TextView tv_shop_level;
        @BindView(R.id.tv_shop_month_sales)
        TextView tv_shop_month_sales;
        @BindView(R.id.tv_open)
        TextView tv_open;
        @BindView(R.id.tv_price)
        TextView tv_price;
        @BindView(R.id.tv_delete)
        TextView tv_delete;
        @BindView(R.id.tv_time)
        TextView tv_time;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.view_line)
        View view_line;


        public MyViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}

